package core;

import java.util.ArrayList;

public class LevenshteinDistance {
        private static int minimum(int a, int b, int c) {
                return Math.min(Math.min(a, b), c);
        }
 
        public static int computeLevenshteinDistance(CharSequence str1,
                        CharSequence str2) {
                int[][] distance = new int[str1.length() + 1][str2.length() + 1];
 
                for (int i = 0; i <= str1.length(); i++)
                        distance[i][0] = i;
                for (int j = 1; j <= str2.length(); j++)
                        distance[0][j] = j;
 
                for (int i = 1; i <= str1.length(); i++)
                        for (int j = 1; j <= str2.length(); j++)
                                distance[i][j] = minimum(
                                                distance[i - 1][j] + 1,
                                                distance[i][j - 1] + 1,
                                                distance[i - 1][j - 1]
                                                                + ((str1.charAt(i - 1) == str2.charAt(j - 1)) ? 0
                                                                                : 1));
 
                return distance[str1.length()][str2.length()];
        }
        
        public static int computeLevenshteinDistance(ArrayList<Dinucleotide> d1, ArrayList<Dinucleotide> d2) {
            String s1 = "", s2 = "";
            for(int i = 0; i < d1.size(); i++) {
                s1 += d1.get(i).getWord();
                s2 += d2.get(i).getWord();
            }
            
            return LevenshteinDistance.computeLevenshteinDistance(s1, s2);
        }
        
        public static double getSimilarity(ArrayList<Dinucleotide> d1, ArrayList<Dinucleotide> d2) {
            String s1 = "", s2 = "";
            for(int i = 0; i < d1.size(); i++) {
                s1 += d1.get(i).getWord();
                s2 += d2.get(i).getWord();
            }
            
            int levenshtein = LevenshteinDistance.computeLevenshteinDistance(s1, s2);
            
            return (((s1.length() - levenshtein) / (double) s1.length()) * 100.0);
        }
        
        public static void computeMinLevenshteinDistance(ArrayList<Dinucleotide> d, ArrayList<ArrayList<Dinucleotide>> list) {
            System.out.println("\n"+d+" - Recherche de similarité avec un X existant");
            int min = 12;
            ArrayList<Dinucleotide> minD = null;
            for(int i = 0; i < list.size(); i++) {
                int distance = LevenshteinDistance.computeLevenshteinDistance(d, list.get(i));
                if(distance < min) {
                    min = distance;
                    minD = list.get(i);
                }
            }
            
            int nbX = Dinucleotide.getMaximalCircularCodeNumber(minD);
            if(nbX != -1) {
                nbX++;
                System.out.println("X"+nbX+" = "+minD+" => distance "+min);
            }
        }
}